﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lucene.Net.Documents;
using CommonSearchModule;
using System.Data.SqlClient;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Index;
using System.Diagnostics;
using System.Configuration;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
namespace CreateIndexFile
{
    public class SearchShiJuanIndexFile : IndexFileBase
    {
        public SearchShiJuanIndexFile()
            : base(ConfigurationManager.AppSettings["ShiJuan_IndexDocumentsPath"])
        { }

        #region Public Methods

        public override void Create()
        {
            //创建分析器
            Stopwatch watch = new Stopwatch();
            watch.Start();
            StringBuilder sb;
            using (SqlConnection conn = new SqlConnection(connString))
            {
                #region SQL代码
                sb = new StringBuilder();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                sb.Append(@"     
    SELECT  distinct  'Res_TeachingPlan'+LTRIM(STR(a.ClassID)) as PrimaryKey, 
             a.ClassID as ItemID  ,
            a.ClassTitle as Name,
            Info='',
            a.PublishTime as PackageTime,
            d.name as CategoryName,
            b.Name,
            b.Name+'|'+c.Name as PropertyName
    FROM    dbo.Res_ShijuanNew a
            INNER JOIN dbo.Common_BasePropValue d ON a.ExamType =d.vid
            INNER JOIN dbo.Common_BasePropValue b ON a.ExamGrade = b.VID and b.PID=1
            INNER JOIN dbo.Common_BasePropValue c ON a.ExamSubject = c.VID and c.PID=2
            AND Flag=1");
                comm.CommandText = sb.ToString();
                conn.Open();
                #endregion
                SqlDataReader reader = comm.ExecuteReader();
                int rowCount = 1;
                try
                {
                    while (reader.Read())
                    {
                        Search_Entity entity = new Search_Entity();
                        entity.PrimaryKey = reader["PrimaryKey"].ToString();
                        entity.ID = reader["ItemID"].ToString();
                        entity.Name = reader["Name"].ToString();
                        entity.Info = Regex.Replace(reader["Info"].ToString(), @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
                        entity.CategoryName = reader["CategoryName"].ToString();
                        entity.PackageTime = reader["PackageTime"].ToString();
                        entity.PropertyName = reader["PropertyName"].ToString();
                        Document doc = InitDocument(entity);//建立索引记录，并设置它的Field类型
                        writer.AddDocument(doc);//添加到索引文件流

                        Console.SetCursorPosition(0, 0);
                        Console.WriteLine("执行次数：" + rowCount++.ToString());
                        Console.WriteLine("执行时间：" + watch.Elapsed.ToString());
                    }
                }
                finally
                {
                    reader.Close();
                }
            }

            writer.Optimize();
            writer.Close();

            TimeSpan _RunTime = new TimeSpan(DateTime.Now.Ticks);
            TimeSpan _RunTime2 = new TimeSpan(DateTime.Now.AddDays(-2).Ticks);
            TimeSpan ss = _RunTime.Subtract(_RunTime2);
            Console.WriteLine(ss.Hours);

            SetCreatePath();
        }

        #endregion Public Methods


    }
}
